package com.china08.yxyapi.api.manage.sch.platform.users.teacher;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "v01", "教师课程-章节排序" })
@CrossOrigin
@RestController
@RequestMapping(value = "/api_yw")
public class YxyLessonsTeaItemSortController extends YxyApiBaseController {
    @Autowired
    private YxyLessonsTopicLessonChapterRepository yxyLessonsTopicLessonChapterRepository;
    @Autowired
    private YxyLessonsSchoolLessonChapterRepository yxyLessonsSchoolLessonChapterRepository;
    @Autowired
    private YxyLessonsSchoolLessonRepository yxyLessonsSchoolLessonRepository;
    @Autowired
    private YxyLessonsSchoolLessonItemRepository yxyLessonsSchoolLessonItemRepository;
    @Autowired
    private YxyLessonsTopicLessonItemRepository yxyLessonsTopicLessonItemRepository;
    @Autowired
    private YxyLessonsTopicLessonRepository yxyLessonsTopicLessonRepository;

    @ApiOperation(value = "教师课程-查看课程")
    @Transactional
    @RequestMapping(value = "/manage/tec/lessons/tea/editor/sortitem.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        int i = 1;
        if (reqModel.clzss == 1) {
            YxyLessonsSchoolLesson lesson = yxyLessonsSchoolLessonRepository.findById(reqModel.lessonId);
            Assert.notNull(lesson, "课程找不到了");
            List<String> chapterIds = new ArrayList<String>();
            List<String> itemsIds = new ArrayList<String>();
            for (ChapterModel chapter : reqModel.chapters) {
                chapterIds.add(chapter.chapterId);
                itemsIds.addAll(chapter.items.stream().map(x -> x.id).collect(Collectors.toList()));
            }
            for (ChapterModel chapterModel : reqModel.chapters) {
                for (Item it : chapterModel.items) {
                    YxyLessonsSchoolLessonItem yxy = yxyLessonsSchoolLessonItemRepository.findById(it.getId());
                    if (!it.getPid().equals(chapterModel.id) && it.getIntro() != null) {
                        it.setPid(chapterModel.id);
                        yxy.setOrderNum(i++);
                        yxy.setChapterId(chapterModel.id);
                    } else {
                        yxy.setOrderNum(i++);
                    }
                    yxyLessonsSchoolLessonItemRepository.saveAndFlush(yxy);
                }
            }
        } else {
            YxyLessonsTopicLesson lesson = yxyLessonsTopicLessonRepository.findById(reqModel.lessonId);
            Assert.notNull(lesson, "课程找不到了");
            List<String> chapterIds = new ArrayList<String>();
            List<String> itemsIds = new ArrayList<String>();
            for (ChapterModel chapter : reqModel.chapters) {
                chapterIds.add(chapter.chapterId);
                itemsIds.addAll(chapter.items.stream().map(x -> x.id).collect(Collectors.toList()));
            }
            for (ChapterModel chapterModel : reqModel.chapters) {
                for (Item it : chapterModel.items) {
                    YxyLessonsTopicLessonItem yxy = yxyLessonsTopicLessonItemRepository.findById(it.getId());
                    if (!it.getPid().equals(chapterModel.id) && it.getIntro() != null) {
                        it.setPid(chapterModel.id);
                        yxy.setChapterId(chapterModel.id);
                        yxy.setOrderNum(i++);
                    } else {
                        yxy.setOrderNum(i++);
                    }
                    yxyLessonsTopicLessonItemRepository.saveAndFlush(yxy);
                }
            }
        }
        return new YxyApiRespModel("操作成功");
    }

    @ApiOperation(value = "教师课程-章节排序")
    @Transactional
    @RequestMapping(value = "/manage/tec/lessons/tea/chapter/sortitem.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        RespModel1 respModel = new RespModel1();
        if (reqModel.clzss == 1) {
            if (reqModel.newNum > reqModel.oldNum) { // up
                for (ChapterModel chapter : reqModel.chapters) {
                    if (chapter.orderNum >= reqModel.oldNum && chapter.orderNum < reqModel.newNum) {
                        chapter.orderNum += 1;
                        YxyLessonsSchoolLessonChapter chap = yxyLessonsSchoolLessonChapterRepository
                                .findOne(chapter.id);
                        if (chapter.orderNum > chap.getOrderNum()) {
                            chap.setOrderNum(chapter.orderNum);
                            yxyLessonsSchoolLessonChapterRepository.saveAndFlush(chap);
                        }
                    }
                }

            } else { // down
                for (ChapterModel chapter : reqModel.chapters) {
                    if (chapter.orderNum > reqModel.newNum && chapter.orderNum <= reqModel.oldNum) {
                        chapter.orderNum -= 1;
                        YxyLessonsSchoolLessonChapter chap = yxyLessonsSchoolLessonChapterRepository
                                .findOne(chapter.id);
                        if (chapter.orderNum < chap.getOrderNum()) {
                            chap.setOrderNum(chapter.orderNum);
                            yxyLessonsSchoolLessonChapterRepository.saveAndFlush(chap);
                        }
                    }
                }
            }
            YxyLessonsSchoolLessonChapter chap = yxyLessonsSchoolLessonChapterRepository.findOne(reqModel.newId);
            chap.setOrderNum(reqModel.oldNum);
            yxyLessonsSchoolLessonChapterRepository.saveAndFlush(chap);
        } else {
            // 专题
            if (reqModel.newNum > reqModel.oldNum) { // up
                for (ChapterModel chapter : reqModel.chapters) {
                    if (chapter.orderNum < reqModel.newNum && chapter.orderNum >= reqModel.oldNum) {
                        chapter.orderNum = chapter.orderNum + 1;
                        YxyLessonsTopicLessonChapter chap = yxyLessonsTopicLessonChapterRepository.findOne(chapter.id);
                        if (chapter.orderNum > chap.getOrderNum()) {
                            chap.setOrderNum(chapter.orderNum);
                            yxyLessonsTopicLessonChapterRepository.saveAndFlush(chap);
                        }
                    }
                }

            } else { // down
                for (ChapterModel chapter : reqModel.chapters) {
                    if (chapter.orderNum > reqModel.newNum && chapter.orderNum <= reqModel.oldNum) {
                        chapter.orderNum = chapter.orderNum - 1;
                        YxyLessonsTopicLessonChapter chap = yxyLessonsTopicLessonChapterRepository.findOne(chapter.id);
                        if (chapter.orderNum < chap.getOrderNum()) {
                            chap.setOrderNum(chapter.orderNum);
                            yxyLessonsTopicLessonChapterRepository.saveAndFlush(chap);
                        }
                    }
                }
            }
            YxyLessonsTopicLessonChapter chap = yxyLessonsTopicLessonChapterRepository.findOne(reqModel.newId);
            chap.setOrderNum(reqModel.oldNum);
            yxyLessonsTopicLessonChapterRepository.saveAndFlush(chap);
        }

        return new YxyApiRespModel("操作成功");
    }

    // 分页请求
    protected static class PageReqModel extends YxyApiPageReqModel {
        public String chapterId;
        public String lessonId; // 课程id
        public String catalogId;// 分类id
        public String title;
        public String intro;
        public Integer status;
    }

    public static class ReqModel extends YxyApiReqModel {
        public List<ChapterModel> chapters;
        public String lessonId;
        public Integer clzss;// 1校本 2专题
        public String oldId; // 排序前
        public String newId; // 排序后
        public Integer oldNum;
        public Integer newNum;
    }

    protected static class RespModel1 extends YxyApiRespModel {
        public List<ChapterModel> chapters;
        public String lessonId;
        public Integer clzss;// 1校本 2专题
    }

    public static class ChapterModel {
        public String id;
        public String chapterId;
        public String title;
        public Integer orderNum;
        public String intro;
        public String lessonId;
        public List<Item> items;
    }

    public static class Item {
        private String pid; // 章节
        private String id; // 课时
        private String title;
        private String intro;
        private Integer status;
        private Integer preview;
        private Integer orderNum;

        /**
         * @return the pid
         */
        public String getPid() {
            return pid;
        }

        /**
         * @param pid
         *            the pid to set
         */
        public void setPid(String pid) {
            this.pid = pid;
        }

        /**
         * @return the id
         */
        public String getId() {
            return id;
        }

        /**
         * @param id
         *            the id to set
         */
        public void setId(String id) {
            this.id = id;
        }

        /**
         * @return the title
         */
        public String getTitle() {
            return title;
        }

        /**
         * @param title
         *            the title to set
         */
        public void setTitle(String title) {
            this.title = title;
        }

        /**
         * @return the intro
         */
        public String getIntro() {
            return intro;
        }

        /**
         * @param intro
         *            the intro to set
         */
        public void setIntro(String intro) {
            this.intro = intro;
        }

        /**
         * @return the status
         */
        public Integer getStatus() {
            return status;
        }

        /**
         * @param status
         *            the status to set
         */
        public void setStatus(Integer status) {
            this.status = status;
        }

        /**
         * @return the preview
         */
        public Integer getPreview() {
            return preview;
        }

        /**
         * @param preview
         *            the preview to set
         */
        public void setPreview(Integer preview) {
            this.preview = preview;
        }

        /**
         * @return the orderNum
         */
        public Integer getOrderNum() {
            return orderNum;
        }

        /**
         * @param orderNum
         *            the orderNum to set
         */
        public void setOrderNum(Integer orderNum) {
            this.orderNum = orderNum;
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        private String id;
        private String lessonId;
        private String catalogId;// 目录id

        private String title;// 章节名称

        private String intro;// 简介

        private String lesson; // 课程
        private Integer orderNum;

        private List<Item> items = new ArrayList<Item>();

        /**
         * @return the id
         */
        public String getId() {
            return id;
        }

        /**
         * @param id
         *            the id to set
         */
        public void setId(String id) {
            this.id = id;
        }

        /**
         * @return the catalogId
         */
        public String getCatalogId() {
            return catalogId;
        }

        /**
         * @param catalogId
         *            the catalogId to set
         */
        public void setCatalogId(String catalogId) {
            this.catalogId = catalogId;
        }

        /**
         * @return the title
         */
        public String getTitle() {
            return title;
        }

        /**
         * @param title
         *            the title to set
         */
        public void setTitle(String title) {
            this.title = title;
        }

        /**
         * @return the intro
         */
        public String getIntro() {
            return intro;
        }

        /**
         * @param intro
         *            the intro to set
         */
        public void setIntro(String intro) {
            this.intro = intro;
        }

        /**
         * @return the lesson
         */
        public String getLesson() {
            return lesson;
        }

        /**
         * @param lesson
         *            the lesson to set
         */
        public void setLesson(String lesson) {
            this.lesson = lesson;
        }

        /**
         * @return the items
         */
        public List<Item> getItems() {
            return items;
        }

        /**
         * @param items
         *            the items to set
         */
        public void setItems(List<Item> items) {
            this.items = items;
        }

        /**
         * @return the lessonId
         */
        public String getLessonId() {
            return lessonId;
        }

        /**
         * @param lessonId
         *            the lessonId to set
         */
        public void setLessonId(String lessonId) {
            this.lessonId = lessonId;
        }

        /**
         * @return the orderNum
         */
        public Integer getOrderNum() {
            return orderNum;
        }

        /**
         * @param orderNum
         *            the orderNum to set
         */
        public void setOrderNum(Integer orderNum) {
            this.orderNum = orderNum;
        }

    }
}
